package com.fingard.dsp.bank.directbank.crbc01;



import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillTrans;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.*;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.detail.*;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import java.util.Date;

/**
 * Created by F on 2019/1/27.
 */
public class CRBC01ElecBillTrans extends CRBC01Base implements IElecBillTrans {
    @Override
    public RetAtsB003BillRespSubmit D_B003_BillResp_Submit(ReqB003BillRespSubmit query) throws Exception {
        RetAtsB003BillRespSubmit retAts = new RetAtsB003BillRespSubmit(query.reqHeader);
        retAts.writeStart();
        query.readRecord();
        RetAtsB003BillRespSubmitDetail detail = new RetAtsB003BillRespSubmitDetail();
        XElement billInfo ;
        try {
            XmlTextWriter xmlWriter =new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110081";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbusstyp	业务类型	★	2	VARCHAR2	"
//            03收票
//            05背书
//            06贴现
//            15质押
//            16质押解除
//            21 追索清偿签收通知"
            String busCode;
            if("1".equals(query.detail.respType)){
                busCode = "02";
                billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,"2","2","");
            }else if("2".equals(query.detail.respType)){
                busCode = "03";
                billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,"2","3","");
            }else if("3".equals(query.detail.respType)){
                busCode ="05";
                billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,"2","4","");
            }else if("5".equals(query.detail.respType)){
                busCode ="16";
                billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,"2","6","");
            }else if("6".equals(query.detail.respType)){
                if("1".equals(query.detail.proPayType)) {
                    busCode = "17";
                    billInfo = getBillInfo(query.detail.billNo, query.detail.actNum, "2", "12", "1");
                }else{
                    busCode = "18";
                    billInfo = getBillInfo(query.detail.billNo, query.detail.actNum, "2", "12", "2");
                }
            }else if("8".equals(query.detail.respType)){
                busCode = "20";
                billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,"2","14", "");
            }else if("9".equals(query.detail.respType)){
                busCode = "15";
                billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,"2","5", "");
            }else if("10".equals(query.detail.respType)){
                busCode = "06";
                billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,"2","10", "");
            }else {
                detail.respCode ="0";
                detail.respInfo = "银行暂不支持该业务类型的签收："+query.detail.respType;
                detail.transState = "paramError";
                detail.payInfoCode = detail.transState;
                retAts.writeDetail(detail);
                retAts.writeEnd();
                return retAts;
            }
            xmlWriter.writeElementString("stdbusstyp",busCode);
//            stdbillnum	电子票据序列号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdbussqno	业务流水标识	★	10	VARCHAR2	从发生查询中带回
            xmlWriter.writeElementString("stdbussqno",query.detail.busiNo);
//            stdsgnrdat	签收日期	★	8	VARCHAR2	YYYYMMDD
            xmlWriter.writeElementString("stdsgnrdat",Format.Date8Format.format(new Date()));
//            stdsgnrres	签收结果	★	4	VARCHAR2	"SU00同意签收 SU01拒绝签收
            xmlWriter.writeElementString("stdsgnrres","1".equals(query.detail.respFlag)?"SU00":"SU01");
//            stdsgnrtyp	签收人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stdsgnrtyp","RC01");
//            stdsgnrnam	签收人名称	★	60	VARCHAR2	发生查询的返回内容
            xmlWriter.writeElementString("stdsgnrnam",billInfo.getSingleText("stdrcvname"));
//            stdsgnrcod	签收人组织机构代码证	★	20	VARCHAR2	发生查询的返回内容
            xmlWriter.writeElementString("stdsgnrcod",billInfo.getSingleText("stdrcvcode"));
//            stdsgnracc	签收人开户帐户	★	32	VARCHAR2	查询交易接收方
            xmlWriter.writeElementString("stdsgnracc",billInfo.getSingleText("stdrcvacct"));
//            stdsgnrbnm	签收人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdsgnrbnm",billInfo.getSingleText("stdrcvbnm"));
            if("1".equals(query.detail.respType)) {
//            stdsgnrrat	签收人信用等级	☆	3	VARCHAR2
                xmlWriter.writeElementString("stdsgnrrat", query.detail.accpCredit);
//            stdsgnrgcy	签收人评级机构	☆	60	VARCHAR2
                xmlWriter.writeElementString("stdsgnrgcy", query.detail.accpGrade);
//            stdsgradat	签收人评级到期日	☆	8	VARCHAR2
                xmlWriter.writeElementString("stdsgradat", DateHelper.convertD10toD8(query.detail.accpGradeDue));
            }else {
                //            stdsgnrrat	签收人信用等级	☆	3	VARCHAR2
                xmlWriter.writeElementString("stdsgnrrat", "");
//            stdsgnrgcy	签收人评级机构	☆	60	VARCHAR2
                xmlWriter.writeElementString("stdsgnrgcy", "");
//            stdsgradat	签收人评级到期日	☆	8	VARCHAR2
                xmlWriter.writeElementString("stdsgradat", "");
            }
//            stdsgnradr	签收人地址	☆	256	VARCHAR2
            xmlWriter.writeElementString("stdsgnradr","");
//            stddshnrcd	拒付理由代码	☆	4	VARCHAR2	"DC00、与自己有直接债权债务关系的持票人未履行约定义务；
//            DC01、持票人以欺诈、偷盗或者胁迫等手段取得票据；
//            DC02、持票人明知有欺诈、偷盗或者胁迫等情形，出于恶意取得票据；
//            DC03、持票人明知债务人与出票人或者持票人的前手之间存在抗辩事由而取得票据；
//            DC04、持票人因重大过失取得不符合《票据法》规定的票据；
//            DC05、超过提示付款期；
//            DC06、被法院冻结或收到法院止付通知书；
//            DC07、票据未到期；
//            DC08、商业承兑汇票承兑人账户余额不足。
//            DC09、其他（必须注明）
//            （提示付款拒绝签收时必填）"
            if("6".equals(query.detail.respType)&&"2".equals(query.detail.respFlag)) {
                if(StringHelper.isNullOrEmpty(query.detail.unPayCode)){
                    throw new Exception("提示付款拒签时，拒付理由不能为空!");
                }
                if("99".equals(query.detail.unPayCode)){
                    xmlWriter.writeElementString("stddshnrcd", "DC09");
                    xmlWriter.writeElementString("stddshnrsn",query.detail.unPayMemo);
                }else{
                    xmlWriter.writeElementString("stddshnrcd", "DC0" + (Integer.valueOf(query.detail.unPayCode) - 1));
                    xmlWriter.writeElementString("stddshnrsn", "");
                }
            }else{
//                stddshnrcd	拒付理由代码	☆
                xmlWriter.writeElementString("stddshnrcd", "");
                //            stddshnrsn	拒付备注	☆	256	VARCHAR2	"拒付代码为DC09时，填写
                xmlWriter.writeElementString("stddshnrsn","");
            }
//            stdconttno	协议号	☆	30	VARCHAR2	承兑,贴现签收时有效
            xmlWriter.writeElementString("stdconttno","");
//            stddrwrsgn	电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stddrwrsgn",query.detail.actNum);
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	回复人备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo","");
            if("6".equals(query.detail.respType)) {
//            autopayflag	是否自动划款	☆	2	VARCHAR2	1：是 0：否（17提示付款、18逾期提示付款 状态下必输线上清算给0）
                String stdstlmthd = billInfo.getSingleText("stdstlmthd");
                xmlWriter.writeElementString("autopayflag","SM01".equals(stdstlmthd)?"0":"1" );
//            isCollztn	是否已质押	☆	2	VARCHAR2	17提示付款、18逾期提示付款 状态下必输，发生查询查出
                xmlWriter.writeElementString("isCollztn", billInfo.getSingleText("isCollztn"));
//            innerflag	是否行内客户	☆	2	VARCHAR2	1：是 0：否（17提示付款、18逾期提示付款 状态下必输，发生查询查出）
                xmlWriter.writeElementString("innerflag", billInfo.getSingleText("innerflag"));
            }else{
                xmlWriter.writeElementString("autopayflag","");
                xmlWriter.writeElementString("isCollztn", "");
                xmlWriter.writeElementString("innerflag", "");
            }
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                        String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                        String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                        detail.respCode ="0";
                        detail.respInfo = resultMsg;
                        detail.transState = resultCode;
                        detail.payInfoCode = detail.transState;
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = tmpsbMsg.toString();
                    }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB101BillApplySubmit D_B101_BillResp_Submit(ReqB101BillApplySubmit query) throws Exception {
        RetAtsB101BillApplySubmit retAts = new RetAtsB101BillApplySubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB101BillApplySubmitDetail detail = new RetAtsB101BillApplySubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110080";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdopertyp	交易类型	★	2	VARCHAR2	"00出票登记 01 一键两功能(结合全流程使用) 02 一键三功能(结合全流程使用)
            String stdopertype;
            if("1".equals(query.detail.autoPromptReceipt)){
                stdopertype = "02";
            }else if("1".equals(query.detail.autoPromptAccept)){
                stdopertype ="01";
            }else{
                stdopertype ="00";
            }
            xmlWriter.writeElementString("stdopertyp",stdopertype);
//            stdbilltyp	票据类型	★	4	VARCHAR2	"AC01银承 AC02商承"
            xmlWriter.writeElementString("stdbilltyp","1".equals(query.detail.billType)?"AC01":"AC02");
//            stdissdate	出票日	★	8	VARCHAR2	YYYYMMDD
            xmlWriter.writeElementString("stdissdate",query.detail.issDate.substring(0,10).replaceAll("-",""));
//            stdduedate	到期日	★	8	VARCHAR2	YYYYMMDD
            xmlWriter.writeElementString("stdduedate",query.detail.dueDate.substring(0,10).replaceAll("-",""));
//            stddrwrtyp	出票人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stddrwrtyp","RC01");
//            stddrwrnam	出票人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stddrwrnam",query.detail.drawName);
//            stddrwrcod	出票人组织机构代码证	★	20	VARCHAR2
            xmlWriter.writeElementString("stddrwrcod",bankFront.org);
//            stddrwracc	出票人开户帐户	★	32	VARCHAR2
            xmlWriter.writeElementString("stddrwracc",query.detail.drawAct);
//            stddrwrbnm	出票人行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stddrwrbnm",query.detail.drawBnkCode);
//            stddrwrsgn	出票人签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stddrwrsgn",query.detail.drawName);
//            stdaccpnam	承兑人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdaccpnam",query.detail.accpName);
//            stdaccpacc	承兑人账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdaccpacc",query.detail.accpAct);
//            stdaccpbnm	承兑人行号	★	12	VARCHAR2	（本行银承为0）
            xmlWriter.writeElementString("stdaccpbnm",query.detail.accpBnkCode);
//            stdaccpamt	票据金额	★	15,2	NUMBER
            xmlWriter.writeElementString("stdaccpamt",String.format("%.2f",Double.valueOf(query.detail.billAmt)));
//            stdendormk	允许背书	★	4	VARCHAR2	"EM00可再转让  EM01不得转让"
            xmlWriter.writeElementString("stdendormk","1".equals(query.detail.transFlag)?"EM00":"EM01");
//            stdpyeenam	收款人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdpyeenam",query.detail.payeeName);
//            stdpyeeacc	收款人开户帐户	★	32	VARCHAR2
            xmlWriter.writeElementString("stdpyeeacc",query.detail.payeeAct);
//            stdpyeebnm	收款人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdpyeebnm",query.detail.payeeBnkCode);
//            stdcontrnm	合同编号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdcontrnm",query.detail.contractNo);
//            stdinvcnum	发票号码	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdinvcnum",query.detail.invoiceNo);
//            stddrcdrat	信用等级	☆	3	VARCHAR2
            xmlWriter.writeElementString("stddrcdrat",query.detail.creditLv);
//            stddrragcy	评级机构	☆	60	VARCHAR2
            xmlWriter.writeElementString("stddrragcy",query.detail.gradeAgent);
//            stdrduedat	评级到期日	☆	8	VARCHAR2
            if(StringHelper.hasAnyChar(query.detail.gradeDueDate)) {
                xmlWriter.writeElementString("stdrduedat", query.detail.gradeDueDate.substring(0,10).replaceAll("-",""));
            }else {
                xmlWriter.writeElementString("stdrduedat","" );
            }
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno",query.detail.reqSeqID);
//            stddrwrmem	出票登记备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("stddrwrmem",query.detail.memo);
//            stdsrce	渠道来源	★	2	VARCHAR2	01：网银  02：银企
            xmlWriter.writeElementString("stdsrce","02");
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB102BillAccpSubmit D_B102_BillResp_Submit(ReqB102BillAccpSubmit query) throws Exception {
        RetAtsB102BillAccpSubmit retAts = new RetAtsB102BillAccpSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        RetAtsB102BillAccpSubmitDetail detail = new RetAtsB102BillAccpSubmitDetail();
        XElement billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,"1","2","");
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110092";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdbilltyp	票据类型	★	4	VARCHAR2	"AC01银承  AC02商承"
            xmlWriter.writeElementString("stdbilltyp","1".equals(query.detail.billType)?"AC01":"AC02");
//            stddrwrtyp	出票人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stddrwrtyp","RC01");
//            stddrwrnam	出票人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stddrwrnam",query.detail.drawName);
//            stddrwrcod	出票人组织机构代码证	★	20	VARCHAR2
            xmlWriter.writeElementString("stddrwrcod",billInfo.getSingleText("stddrwrcod"));
//            stddrwracc	出票人开户帐户	★	32	VARCHAR2
            xmlWriter.writeElementString("stddrwracc",query.detail.actNum);
//            stddrwrbnm	出票人行号	☆	12	VARCHAR2
            xmlWriter.writeElementString("stddrwrbnm",query.detail.drawBnkCode);
//            stddrwrsgn	出票人签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stddrwrsgn",query.detail.drawName);
//            stdaccpnam	承兑人名称	★	60	VARCHAR2	?可以通过查询获得
            xmlWriter.writeElementString("stdaccpnam",billInfo.getSingleText("stdaccpnam"));
//            stdaccpacc	承兑人账号	★	32	VARCHAR2	（本行银承为0）
            xmlWriter.writeElementString("stdaccpacc",billInfo.getSingleText("stdaccpacc"));
//            stdaccpbnm	承兑人行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdaccpbnm",billInfo.getSingleText("stdaccpbnm"));
//            stdaccpamt	申请承兑金额	★	15,2	NUMBER
            xmlWriter.writeElementString("stdaccpamt",String.format("%.2f",Double.valueOf(query.detail.billAmt)));
//            stdendormk	允许背书	☆	4	VARCHAR2	"EM00可再转让 EM01不得转让"
            xmlWriter.writeElementString("stdendormk","");
//            stdpyeenam	收款人名称	☆	60	VARCHAR2
            xmlWriter.writeElementString("stdpyeenam",query.detail.payeeName);
//            stdpyeeacc	收款人开户帐户	☆	32	VARCHAR2
            xmlWriter.writeElementString("stdpyeeacc",query.detail.payeeAct);
//            stdpyeebnm	收款人开户行行号	☆	12	VARCHAR2
            xmlWriter.writeElementString("stdpyeebnm",query.detail.payeeBnkCode);
//            stdcontrnm	合同编号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdcontrnm","");
//            stdinvcnum	发票号码	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdinvcnum","");
//            stdbatchnm	批次号	☆	10	VARCHAR2
            xmlWriter.writeElementString("stdbatchnm","");
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB103BillRetSubmit D_B103_BillResp_Submit(ReqB103BillRetSubmit query) throws Exception {
        RetAtsB103BillRetSubmit retAts = new RetAtsB103BillRetSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        query.readRecord();
        RetAtsB103BillRetSubmitDetail detail = new RetAtsB103BillRetSubmitDetail();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110082";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票据序列号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stddrwracc	出票人开户帐户	★	32	VARCHAR2
            xmlWriter.writeElementString("stddrwracc",query.detail.actNum);
//            stdcobksgn	出票人电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stdcobksgn",query.detail.actNum);
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB104BillReceSubmit D_B104_BillResp_Submit(ReqB104BillReceSubmit query) throws Exception {
        RetAtsB104BillReceSubmit retAts = new RetAtsB104BillReceSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB104BillReceSubmitDetail detail = new RetAtsB104BillReceSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110093";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdbilltyp	票据类型	★	4	VARCHAR2	"AC01银承 AC02商承"
            xmlWriter.writeElementString("stdbilltyp","1".equals(query.detail.billType)?"AC01":"AC02");
//            stddrwrtyp	出票人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stddrwrtyp","RC01");
//            stddrwrnam	出票人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stddrwrnam",query.detail.billOpenName);
//            stddrwrcod	出票人组织机构代码证	★	20	VARCHAR2
            xmlWriter.writeElementString("stddrwrcod",bankFront.org);
//            stddrwracc	出票人开户帐户	★	30	VARCHAR2
            xmlWriter.writeElementString("stddrwracc",query.detail.actNum);
//            stddrwrbnm	出票人行号	☆	12	VARCHAR2
            xmlWriter.writeElementString("stddrwrbnm",query.detail.actBnkCode);
//            stddrwrsgn	出票人签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stddrwrsgn",query.detail.actNum);
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB105BillEndoSubmit D_B105_BillResp_Submit(ReqB105BillEndoSubmit query) throws Exception {
        RetAtsB105BillEndoSubmit retAts = new RetAtsB105BillEndoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB105BillEndoSubmitDetail detail = new RetAtsB105BillEndoSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110098";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdpmmoney	票面金额	★	15,2	NUMBER
            xmlWriter.writeElementString("stdpmmoney",String.format("%.2f",Double.valueOf(query.detail.billAmt)));
//            stdendrnam	背书人全称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdendrnam",query.detail.endoName);
//            stdendrtyp	背书人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stdendrtyp","RC01");
//            stdendrcod	背书人组织机构代码证	★	20	VARCHAR2	查询回来（接收人）
            xmlWriter.writeElementString("stdendrcod",bankFront.org);
//            stdendracc	背书人账号	★	32	VARCHAR2	查询回来的账号
            xmlWriter.writeElementString("stdendracc",query.detail.actNum);
//            stdendrbnm	背书人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdendrbnm",query.detail.endoBnkCode);
//            stdendrsgn	背书人电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stdendrsgn",query.detail.endoName);
//            stdappldat	背书申请日期	★	8	VARCHAR2
            xmlWriter.writeElementString("stdappldat", Format.Date8Format.format(new Date()));
//            stdbanmflg	不得转让标记	★	4	VARCHAR2	"EM00可再转让
            xmlWriter.writeElementString("stdbanmflg","1".equals(query.detail.transFlag)?"EM00":"EM01");
//            EM01不得转让"
//            stdendenam	被背书人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdendenam",query.detail.receName);
//            stdendeacc	被背书人账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdendeacc",query.detail.receAct);
//            stdendebnm	被背书人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdendebnm",query.detail.receBnkCode);
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB106BillDiscSubmit D_B106_BillResp_Submit(ReqB106BillDiscSubmit query) throws Exception {
        RetAtsB106BillDiscSubmit retAts = new RetAtsB106BillDiscSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB106BillDiscSubmitDetail detail = new RetAtsB106BillDiscSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110095";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票据序列号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdpmmoney	票面金额	★	20	VARCHAR2
            xmlWriter.writeElementString("stdpmmoney",String.format("%.2f",Double.valueOf(query.detail.billAmt)));
//            stddsnttyp	贴现类型	★	4	VARCHAR2	RM00买断式
            String stddsnttype = "1".equals(query.detail.discType)?"RM00":"RM01";
            xmlWriter.writeElementString("stddsnttyp",stddsnttype);
//            stdopertyp	交易类型	★	4	VARCHAR2	TT00贴现
            xmlWriter.writeElementString("stdopertyp","TT00");
//            stddscntdt	申请贴现日期	★	8	VARCHAR2
            xmlWriter.writeElementString("stddscntdt", Format.Date8Format.format(new Date()));
//            stddscntrt	贴现利率	★	9,7	NUMBER
            xmlWriter.writeElementString("stddscntrt",query.detail.discRate);
//            stdintemtd	付息方式	★	2	VARCHAR2	"直贴贴入行为我行时有效
//            01买方付息
//            02卖方付息
//            03协议付息
//            04第三方付息（默认卖方付息）"
                if("1".equals(query.detail.payInterestType)){
                xmlWriter.writeElementString("stdintemtd","02");
            }else if ("2".equals(query.detail.payInterestType)){
                xmlWriter.writeElementString("stdintemtd","01");
            }else if("3".equals(query.detail.payInterestType)){
                xmlWriter.writeElementString("stdintemtd","03");
            }else{
                xmlWriter.writeElementString("stdintemtd","02");
            }
//            stdaoaiacc	入账账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdaoaiacc",query.detail.actedAct);
//            stdaoaibnm	入账行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdaoaibnm",query.detail.actedBnkCode);
//            stdcontnum	交易合同编号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdcontnum","");
//            stdinvonum	发票号码	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdinvonum","");
//            stdbnedrmt	不得转让标记	★	4	VARCHAR2	"EM00可再转让
            if("2".equals(query.detail.discType)&&!"1".equals(query.detail.transFlag)){
                detail.respCode ="0";
                detail.respInfo = "回购式贴现不支持不可转让";
                detail.transState = "paramError";
                detail.payInfoCode = detail.transState;
                retAts.writeDetail(detail);
                retAts.writeEnd();
                return retAts;
            }
            xmlWriter.writeElementString("stdbnedrmt","1".equals(query.detail.transFlag)?"EM00":"EM01");
//            EM01不得转让
//            如为“回购式”不得填“EM01”"
//            stdstlmthd 	清算方式	★	4	VARCHAR2	"SM00线上清算
//            SM01线下清算"
            xmlWriter.writeElementString("stdstlmthd","1".equals(query.detail.payType)?"SM00":"SM01");
//            stdproptyp	贴出人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stdproptyp","RC01");
//            stdpropnme	贴出人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdpropnme",query.detail.discName);
//            stdpropcod	贴出人组织机构代码证号	★	20	VARCHAR2
            xmlWriter.writeElementString("stdpropcod",bankFront.org);
//            stdpropacc	贴出人账户	★	32	VARCHAR2
            xmlWriter.writeElementString("stdpropacc",query.detail.actNum);
//            stdpropbnm	贴出人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdpropbnm",query.detail.discBnkCode);
//            stdpropsgn	贴出人电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stdpropsgn",query.detail.actNum);
//            stddsbknme	贴入人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stddsbknme",query.detail.receName);
//            stddsbkacc	贴入人账户	★	32	VARCHAR2
            xmlWriter.writeElementString("stddsbkacc",query.detail.receAct);
//            stddsbkbnm	贴入人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stddsbkbnm",query.detail.receBnkCode);
//            stdbatchnm	批次号	☆	10	VARCHAR2
            xmlWriter.writeElementString("stdbatchnm","");
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB107BillPledSubmit D_B107_BillResp_Submit(ReqB107BillPledSubmit query) throws Exception {
        RetAtsB107BillPledSubmit retAts = new RetAtsB107BillPledSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB107BillPledSubmitDetail detail = new RetAtsB107BillPledSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110096";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票据序列号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdpmmoney	票面金额	★	15,2	NUMBER
            xmlWriter.writeElementString("stdpmmoney",String.format("%.2f",Double.valueOf(query.detail.billAmt)));
//            stdcoltnam	出质人全称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdcoltnam",query.detail.pledName);
//            stdcolttyp	出质人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stdcolttyp","RC01");
//            stdcoltcod	出质人组织机构代码	★	20	VARCHAR2
            xmlWriter.writeElementString("stdcoltcod",bankFront.org);
//            stdcoltacc	出质人账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdcoltacc",query.detail.actNum);
//            stdcoltbnm	出质人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdcoltbnm",query.detail.pledBnkCode);
//            stdcobknam	质权人全称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdcobknam",query.detail.receName);
//            stdcobkacc	质权人账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdcobkacc",query.detail.receAct);
//            stdcobkbnm	质权人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdcobkbnm",query.detail.receBnkCode);
//            stdcoltdat	质押申请日期	★	8	VARCHAR2	YYYYMMDD
            xmlWriter.writeElementString("stdcoltdat",Format.Date8Format.format(new Date()));
//            stdcoltsgn	出质人电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stdcoltsgn",query.detail.pledName);
//            stdbatchnm	批次号	☆	10	VARCHAR2
            xmlWriter.writeElementString("stdbatchnm","");
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB108BillPaySubmit D_B108_BillResp_Submit(ReqB108BillPaySubmit query) throws Exception {
        RetAtsB108BillPaySubmit retAts = new RetAtsB108BillPaySubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB108BillPaySubmitDetail detail = new RetAtsB108BillPaySubmitDetail();
        query.readRecord();
        XElement billInfo = getBillInfo(query.detail.billNo,query.detail.actNum,
                "1","12",query.detail.proPayType);
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110100";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdbusstyp	提示付款类型	★	2	VARCHAR2	"01正常 02逾期"
            String stdbusstyp = "1".equals(query.detail.proPayType)?"01":"02";
            xmlWriter.writeElementString("stdbusstyp",stdbusstyp);
//            stdprsnacc	提示付款人账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdprsnacc",query.detail.actNum);
//            stdprsnsgn	提示付款人电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stdprsnsgn",query.detail.actName);
//            stdappldte	提示付款申请日期	★	8	VARCHAR2
            xmlWriter.writeElementString("stdappldte",Format.Date8Format.format(new Date()));
//            stdsttlflg	线上清算标志	★	4	VARCHAR2	"SM00：线上清算 SM01：线下清算
            String stdsttlflg;
            if("1".equals(query.detail.payType)){
                String acpBnkNm = billInfo.getSingleText("stdaccpnam");
               if(StringHelper.hasAnyChar(acpBnkNm)&&acpBnkNm.contains("华润")){
                   detail.respCode ="0";
                   detail.respInfo = "承兑人是华润银行不支持线上清算";
                   detail.transState = "paramError";
                   detail.payInfoCode = detail.transState;
                   retAts.writeDetail(detail);
                   retAts.writeEnd();
                   return retAts;
               }
                stdsttlflg = "SM00";
            }else{
                stdsttlflg = "SM01";
            }
            xmlWriter.writeElementString("stdsttlflg",stdsttlflg);
//            注：我行为承兑人的只能选择线下清算"
//            stdoduersn	逾期说明	☆	256	VARCHAR2	（逾期时必须输入）
            xmlWriter.writeElementString("stdoduersn",query.detail.overDueRea);
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno",query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB109BillGuarSubmit D_B109_BillResp_Submit(ReqB109BillGuarSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB110BillRecoSubmit D_B110_BillResp_Submit(ReqB110BillRecoSubmit query) throws Exception {
        RetAtsB110BillRecoSubmit retAts = new RetAtsB110BillRecoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB110BillRecoSubmitDetail detail = new RetAtsB110BillRecoSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110101";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdbusstyp	追索类型	★	4	VARCHAR2	"RT00拒付追索
//            RT01非拒付追索"
            String recoType = "1".equals(query.detail.recourseType)?"RT00":"RT01";
            xmlWriter.writeElementString("stdbusstyp",recoType);
//            stdappldte	追索申请日期	★	8	VARCHAR2
            xmlWriter.writeElementString("stdappldte",Format.Date8Format.format(new Date()));
//            stdppayamt	追索金额	★	15,2	NUMBER
            xmlWriter.writeElementString("stdppayamt",String.format("%.2f",Double.valueOf(query.detail.recourseAmt)));
//            stdrcrstyp	追索人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stdrcrstyp","RC01");
//            stdrcrsacc	追索人账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdrcrsacc",query.detail.actNum);
//            stdrcrssgn	追索人电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stdrcrssgn",query.detail.recoName);
//            stdrcvgnam	被追索人名称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdrcvgnam",query.detail.receName);
//            stdrecrcod	被追索人组织机构代码	★	20	VARCHAR2
            xmlWriter.writeElementString("stdrecrcod",query.detail.receCode);
//            stdrcvgacc	被追索人账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdrcvgacc",query.detail.receAct);
//            stdrcvgbnm	被追索人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdrcvgbnm",query.detail.receBnkCode);
//            stdrcrsbcd	追索理由代码	☆	4	VARCHAR2	"非拒付追索时填写
            if("RT01".equals(recoType)) {
                xmlWriter.writeElementString("stdrcrsbcd", "1".equals(query.detail.recourseCode)?"RC00":"RC01");
            }else{
                xmlWriter.writeElementString("stdrcrsbcd","" );
            }
//            RC00承兑人被依法宣告破产
//            RC01承兑人因违法被责令终止活动"
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);


            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB111BillAgreeSubmit D_B111_BillResp_Submit(ReqB111BillAgreeSubmit query) throws Exception {
        RetAtsB111BillAgreeSubmit retAts = new RetAtsB111BillAgreeSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB111BillAgreeSubmitDetail detail = new RetAtsB111BillAgreeSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110102";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdapersgn	同意清偿人电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stdapersgn",query.detail.actNum);
//            stdagpydat	同意清偿日期	★	8	VARCHAR2
            xmlWriter.writeElementString("stdagpydat",Format.Date8Format.format(new Date()));
//            stdapayamt	同意清偿金额	★	15,2	NUMBER
            xmlWriter.writeElementString("stdapayamt",String.format("%.2f",Double.valueOf(query.detail.agreeAmt)));
//            stdbussqno	业务流水标识	★	10	VARCHAR2	从发生查询中带回
            xmlWriter.writeElementString("stdbussqno",query.detail.busiNo);
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     *
     * @param billNo 票据号
     * @param actNum 操作账号
     * @param operType 操作类型 1-申请 2-签收 3-撤销
     * @param busiNo 业务标识
     * @param isOver 1-正常或者拒付 2-逾期或非拒付
     * @return 票据信息
     */
    private XElement getBillInfo(String billNo, String actNum, String operType, String busiNo,String isOver)throws Exception {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranTime = new Date();
            header.tranCode = "110084";
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdqrycont	查询内容	★	2	VARCHAR2	"23 出票查询
//            04 - 撤票
//            05 - 背书
//            06 - 贴现
//            15 - 质押
//            16 - 质押解除
//            17 - 提示付款
//            18 - 逾期提示付款
//            19 - 拒付追索通知
//            20 - 非拒付追索
//            21 - 追索同意清偿请求"
            String buscode;
            switch (Integer.valueOf(busiNo)){
                case 1:buscode = "04";break;
                case 2:buscode = "23";break;
                case 3:buscode = "23";break;
                case 4:buscode = "05";break;
                case 5:buscode = "15";break;
                case 6:buscode = "16";break;
                case 10:buscode = "06";break;
                case 11:buscode = "07";break;
                case 12:
                    if(!"2".equals(operType)){
                          if("1".equals(isOver)){
                      buscode = "17";
                      }else{
                      buscode = "18";
                      }
                    }else{
                        buscode = "24";
                    }break;
                case 13:
                    if("1".equals(isOver)) {
                        buscode = "19";
                    }else{
                        buscode = "20";
                    }break;
                case 14:buscode = "21";break;
                default:throw new Exception("未知的业务类型："+busiNo);
            }
            String stdtrastat;
            String stdsendflg="01";
            if("3".equals(operType)){
                stdtrastat ="1";
                if("1".equals(busiNo)){
                    stdtrastat = "0";
                }
            }else{
                stdtrastat ="0";
                if("2".equals(operType)) {
                    stdsendflg = "02";
                }
            }
            xmlWriter.writeElementString("stdqrycont",buscode);
//            stdtrastat	交易状态	★	1	VARCHAR2	"0可操作（发起申请交易时）
            xmlWriter.writeElementString("stdtrastat",stdtrastat);
//            1正在操作（撤销申请交易时）"
//            stdsendflg	发送方标志	★	2	VARCHAR2	"01发起方（查询所有可发起的交易）
            xmlWriter.writeElementString("stdsendflg",stdsendflg);
//            02接收方（查询所有可签收的交易）"
//            stdcustcod	客户组织机构代码证	★	20	VARCHAR2	"对于发送方标志为01发起方时填写
            if("01".equals(stdsendflg)){
                xmlWriter.writeElementString("stdcustcod",bankFront.org);
            }else {
                xmlWriter.writeElementString("stdcustcod", "");
            }
//            对于发送方标志为02接收方时不填写"
//            stdcustacc	账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdcustacc",actNum);
//            stdcustbnm	客户行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdcustbnm","");
//            stdbilattr	票据属性	★	1	VARCHAR2	2-电子
            xmlWriter.writeElementString("stdbilattr","2");
//            stdbillnum	电子票据序列号		30	VARCHAR2	票据号码
            xmlWriter.writeElementString("stdbillnum", billNo);
//        BEGINPOS	翻页数据起始位置	★	10	VARCHAR2
        xmlWriter.writeElementString("BEGINPOS", "1");
//        SHOWNUM	翻页一次显示数量	★	10	VARCHAR2
        xmlWriter.writeElementString("SHOWNUM", "10");
//        stdtransno	交易流水号	★	30	VARCHAR2
        xmlWriter.writeElementString("stdtransno",getQryTransID());
        xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_Msg");
                    if("000000".equals(resultCode)){
                        while (tmpBkReader.read("/CRBANK/BODY/bill_array/STRUCT")){
                            //stdinfotyp
                                String queryType = tmpBkReader.readedXElement.getSingleText("stdinfotyp");
                            if("2".equals(busiNo)&&!"02".equals(queryType)){
                                continue;
                            }else if("3".equals(busiNo)&&!"03".equals(queryType)){
                                continue;
                            }
                            return tmpBkReader.readedXElement;
                        }

                    }else{
                        throw new Exception(resultMsg);
                    }
                }else{
                    throw new Exception(tmpsbMsg.toString());
                }
            }else{
                throw new Exception(bankResult[1]);
            }
        return null;
    }

    @Override
    public RetAtsB112BillCancSubmit D_B112_BillResp_Submit(ReqB112BillCancSubmit query) throws Exception {
        RetAtsB112BillCancSubmit retAts = new RetAtsB112BillCancSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            header.tranCode = "110094";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票据序列号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo);
//            stdpmmoney	票面金额	★	15,2	NUMBER
            xmlWriter.writeElementString("stdpmmoney",String.format("%.2f",Double.valueOf(query.detail.billAmt)));
//            stdtrandat	交易日期	★	8	VARCHAR2	YYYYMMDD
            xmlWriter.writeElementString("stdtrandat",Format.Date8Format.format(new Date()));
//            stdtrannum	交易流水号	★	6	VARCHAR2
            xmlWriter.writeElementString("stdtrannum",query.detail.busiNo);
//            stdrvkrtyp	撤销人类型	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stdrvkrtyp","RC01");
//            stdrvkrcod	撤销人组织机构代码	★	20	VARCHAR2
            xmlWriter.writeElementString("stdrvkrcod",bankFront.org);
//            stdrvkracc	撤销人开户帐户	★	32	VARCHAR2
            xmlWriter.writeElementString("stdrvkracc",query.detail.actNum);
//            stdrvkrbnm	撤销人行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdrvkrbnm",query.detail.cancBnkCode);
//            stddrwrsgn	电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stddrwrsgn",query.detail.actNum);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo",query.detail.memo);
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno",query.detail.reqSeqID);

            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB113ProtocolSubmit D_B113_ProtocolResp_Submit(ReqB113ProtocolSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB119BillRelePledSubmit D_B119_BillResp_Submit(ReqB119BillRelePledSubmit query) throws Exception {
       RetAtsB119BillRelePledSubmit retAts = new RetAtsB119BillRelePledSubmit(query.reqHeader);
        retAts.writeStart();
        RetAtsB119BillRelePledSubmitDetail detail = new RetAtsB119BillRelePledSubmitDetail();
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header header = new CRBC01Header();
            //质押解除交易码
            header.tranCode = "110097";
            header.tranTime = new Date();
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("BODY");
//            stdbillnum	电子票据序列号	★	30	VARCHAR2
            xmlWriter.writeElementString("stdbillnum",query.detail.billNo );
//            stdpmmoney	票面金额	★	15,2	NUMBER
            xmlWriter.writeElementString("stdpmmoney",String.format("%.2f",Double.valueOf(query.detail.billAmt)));
//            stdcoltdat	质押解除申请日期	★	8	VARCHAR2	YYYYMMDD
            xmlWriter.writeElementString("stdcoltdat", Format.Date8Format.format(new Date()));
//            stdcobktyp	质权人类别	★	4	VARCHAR2	RC01企业
            xmlWriter.writeElementString("stdcobktyp", "RC01");
//            stdcobkcod	质权人组织机构代码	★	20	VARCHAR2
            xmlWriter.writeElementString("stdcobkcod", bankFront.org);
//            stdcobknam	质权人全称	★	60	VARCHAR2
            xmlWriter.writeElementString("stdcobknam", query.detail.pledgeeName);
//            stdcobksgn	电子签名	★	2000	VARCHAR2
            xmlWriter.writeElementString("stdcobksgn", query.detail.pledgeeName);
//            stdcobkacc	质权人账号	★	32	VARCHAR2
            xmlWriter.writeElementString("stdcobkacc", query.detail.actNum);
//            stdcobkbnm	质权人开户行行号	★	12	VARCHAR2
            xmlWriter.writeElementString("stdcobkbnm", query.detail.pledgeeBnkCode);
//            stdbatchnm	批次号	☆	10	VARCHAR2
            xmlWriter.writeElementString("stdbatchnm", "");
//            stdtransno	交易流水号	☆	30	VARCHAR2
            xmlWriter.writeElementString("stdtransno", query.detail.reqSeqID);
//            std400memo	备注	☆	256	VARCHAR2
            xmlWriter.writeElementString("std400memo", query.detail.memo);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString());
            if(bankResult[0].length() ==0){
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpsbMsg);
                if(tmpBkReader!=null){
                    String resultCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String resultMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    detail.respCode ="0";
                    detail.respInfo = resultMsg;
                    detail.transState = resultCode;
                    detail.payInfoCode = detail.transState;
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpsbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex ){
            ex.printStackTrace();
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }


}
